#!/bin/bash

# Input validation

if [ ! $# -gt 0 ]; then
  echo "Usage: $0 input output" > /dev/stderr
  exit 1
fi

#Determine paths
SCRIPT_PATH=`dirname "$0"`
SCRIPT_PATH=`cd $SCRIPT_PATH; pwd`
LIB_PATH=`cd $SCRIPT_PATH/../lib; pwd`

#Include common functions
source $SCRIPT_PATH/common-functions

ensure_java_precense

# Main program

# Create tmp ast
TMP_AST=$1".ast"
generate_abstract_syntax_tree $1 $TMP_AST > /dev/null

# Compile
if [ $# == 2 ]; then
	java -jar $LIB_PATH/compiler-${version}-bin.jar -i $TMP_AST -o $2
else
	java -jar $LIB_PATH/compiler-${version}-bin.jar -i $TMP_AST
fi

#Clean up
rm $TMP_AST

